<?php
/**
 * Created by PhpStorm.
 * User: fenfen
 * Date: 2016/11/17
 * Time: 10:45
 */

namespace backend\controllers;

use bc\controller\BackendController;
use bc\model\wc\HospitalModel;
use bc\util\backend\Page;

class HospitalController extends BackendController
{
    public function actionIndex()
    {
        $where = array (
            'is_invalid' => 1
        );

        $page = $this->get('page', 1);

        $hospitalM = new HospitalModel();
        $hospitalM->columns = 'id,name,address,level,create_time';
        $hospitalM->sort = 'id desc';
        $hospitalList = $hospitalM->getList( $where,$page,self::PAGE_SIZE);
        $total = $hospitalM->getTotal($where);
        $pagination = new Page($total, self::PAGE_SIZE);
        $row['pagination'] = $pagination;
        $row ['hospitalList'] = $hospitalList;
        return $this->render('index',$row);
    }

    /*
     * 编辑
     * */
    public function actionEdit()
    {
        $id = $this->get('id');
        $model = new HospitalModel();
        $row=$model->getRowById($id);
        $data['row']= $row;
        return $this->render('edit',$data);

    }

    /*
 * 保存
 * */
    public function actionSave()
    {
        $id = $this->post('id');
        $name = $this->post('name');
        $level = $this->post('level');
        $address = $this->post('address');
        $data['name'] = $name;
        $data['level'] = $level;
        $data['address'] = $address;
        $model = new HospitalModel();
        if(empty($id))
        {
            $data['create_time']=time();
            $data['update_time']=time();
            $data['is_invalid']=1;
            $model->add($data);
        }else {
            $data['update_time']=time();
            $model->modify($data, $id);
        }
        $this->redirect('/hospital/index');
    }

    /*
     * 删除
     * */
    public function actionDelet()
    {
        $id = $this->get('id');
        $model = new HospitalModel();
        $data['update_time']=time();
        $data['is_invalid']=0;
        $model->modify($data, $id);
        $this->redirect('/hospital/index');
    }

    /*
     * 添加
     * */
    public function actionAdd()
    {
        return $this->render('add');
    }

}